//
//  Object+LSF.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/3/14.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
///对象转Json字符串
func lsf_toJson(_ object: Any?) -> String? {
    guard let object = object else {
        return nil
    }
    
    if JSONSerialization.isValidJSONObject(object) == false {
        return nil
    }
    
    guard let data = try? JSONSerialization.data(withJSONObject: object, options: []) else {
        return nil
    }
    let jsonString = String.init(data: data, encoding: String.Encoding.utf8)
    return jsonString
}


///Json转对象
func lsf_jsonToObject(_ json: String?) -> Any? {
    guard let json = json else {
        return nil
    }
    
    guard let data = json.data(using: String.Encoding.utf8) else {
        return nil
    }
    
    let object = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
    
    return object
}
